from .targets import Targets
from .scan import Scan
from .scanning_profiles import ScanningProfile
from .vulnerabilities import Vulnerabilities
from export.export import export_to_excel
from config.settings import AWVS_URL, AWVS_API

class AWVSScan(Targets, Scan, ScanningProfile, Vulnerabilities):
    def workflow_targets(self, targets:list):
        """将存活添加到扫描器中 """
        return self.add_targets(targets)
    
    def workflow_vuln_details(self, target_id: str):
        vuln_abbr = self.get_vuln(target_id)
        vulns = []
        for vuln in vuln_abbr:
            vuln_id = vuln['vuln_id']
            vuln_details = self.get_vulnerabilities_details(vuln_id)
            vulns.append(vuln_details)
        return vulns
    
    def export(self,results, is_save, save_path):
        if not is_save:
            return
        export_to_excel(results, sheet_name='awvs信息(扫描中)', xlsx_name='awvs', save_path=save_path)
    
    def run(self, alive_domains, is_save=True, save_path=None):
        results = self.workflow_targets(alive_domains)
        export_results = [ {'url': x['address'], 'target_id': x['target_id']} for x in results ]
        self.export_results = export_results
        if results:
            print(results)
            self.export(export_results, is_save, save_path)
        else:
            print('AWVS 添加扫描器失败')

class AWVS:
    def __init__(self):
        self.awvs = AWVSScan(AWVS_URL, AWVS_API)
    
    def run(self, alive_domains, is_save=True, save_path=None):
        self.awvs.run(alive_domains, is_save, save_path)
